package com.xyoto.community.config;

import com.xyoto.community.listener.RedisExpiredKeyListener;
import com.xyoto.community.mapper.picture.PictureMapper;
import com.xyoto.community.service.PictureService;
import com.xyoto.community.utils.RedisCommonUtils;
import com.xyoto.community.utils.XYOtoOss;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.Resource;

/**
 * @author coderFu
 * @create 2021/10/21
 */
@Configuration
public class RedisExpireKeyListener {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    @Autowired
    private PictureMapper pictureMapper;

    @Resource
    private XYOtoOss xyOtoOss;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //注入监听者的实例
        RedisExpiredKeyListener redisExpiredKeyListener = new RedisExpiredKeyListener();
        redisExpiredKeyListener.setPictureMapper(pictureMapper);
        redisExpiredKeyListener.setPictureService(pictureService);
        redisExpiredKeyListener.setRedisCommonUtils(redisCommonUtils);
        redisExpiredKeyListener.setXyOtoOss(xyOtoOss);
        container.addMessageListener(redisExpiredKeyListener,new PatternTopic("__keyevent@0__:expired"));
        return container;
    }
}
